﻿using Autofac.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Cordyceps.Micro.Core.Extensions;

// ────────────────────────────────────────────────────
// ⚙️ 扩展方法：统一生命周期注册
// ────────────────────────────────────────────────────

internal static class RegistrationLifetimeExtensions
{
    public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle>
        WithLifetime<TLimit, TActivatorData, TRegistrationStyle>(
            this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registration,
            ServiceLifetime lifetime)
        where TRegistrationStyle : SingleRegistrationStyle
    {
        return lifetime switch
        {
            ServiceLifetime.Transient => registration.InstancePerDependency(),
            ServiceLifetime.Scoped => registration.InstancePerLifetimeScope(),
            ServiceLifetime.Singleton => registration.SingleInstance(),
            _ => throw new ArgumentOutOfRangeException(nameof(lifetime))
        };
    }
}
